﻿using System.Windows.Forms;
using ApplicationLibrary;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.Preferences;
using Plumber.Accounting.Forms;
using Plumber.Accounting.ModuleData;
using Plumber.Accounting.Preferences;
using Plumber.Masters.ModuleData;
using Plumber.Sales.ModuleData;

namespace Plumber.Accounting.Scrounger
{
    public partial class TabJE : UserControl, iScroungerUserControl
    {
        private const string JournalEntrySlave = "JOURNALENTRSLAVE";
        private Sales.Forms.SalesInvoice saleForm = null;
        private AccountingPref AccountingPref = null;
        private DRJournalEntry drJournalEntry = null;

        public TabJE()
        {
            InitializeComponent();
            AccountingPref = new PreferencesManager<AccountingPref>(this.Name, null).ReadPreference();
            drJournalEntry = new DRJournalEntry(false);
        }

        public void OnAttachData(ApplicationLibrary.DBManager.DBManager dbManager)
        {
            dgwJE.AutoGenerateColumns = false;
            dgwJE.DataSource = dbManager.Dataset.Tables[JournalEntrySlave];
            dgwJE.AddReadOnlyColumn(PL_JournalEntry.JournalNo, true);
            dgwJE.AddReadOnlyColumn(PL_JournalEntry.PostingDate, true);
            dgwJE.AddReadOnlyColumn(PL_JournalEntryDetail.Description, true);
            dgwJE.AddReadOnlyColumn(PL_JournalEntryDetail.From, true);
            dgwJE.AddReadOnlyColumn(PL_JournalEntryDetail.To, true);

            ntbFrom.Prefix = Properties.Resources.CurrencySimbol;
            ntbTo.Prefix = Properties.Resources.CurrencySimbol;
        }

        public void OnBindData(ApplicationLibrary.DBManager.iScrounger frm)
        {
            DocumentForm doc = (DocumentForm)frm;

            this.saleForm = (Sales.Forms.SalesInvoice)frm;
            doc.BindControl(dgwJE);
            doc.BindColumn(jec_JENo, PL_JournalEntry.JournalNo);
            doc.BindColumn(jec_Date, PL_JournalEntry.PostingDate);
            doc.BindColumn(jec_Desc, PL_JournalEntryDetail.Description);
            doc.BindColumn(jec_From, PL_JournalEntryDetail.From);
            doc.BindColumn(jec_To, PL_JournalEntryDetail.To);
        }

        private void dgwInvoice_DataGridViewDoubleClickCell(object sender, DataGridViewDoubleClickArgs e)
        {
            if (e.ColumnName == PL_SalesInvoices.InvoiceNo.Name)
            {
                Sales.Forms.SalesInvoice frm = (Sales.Forms.SalesInvoice)GlobalInfo.OpenForm("Plumber.Plumber.Sales.Forms.SalesInvoice", false);
                frm.FindRecord(new RadarSaleInvoicesParam(e.CellValue));
            }
            if (e.ColumnName == PL_SalesInvoices.Master.Name)
            {
                Masters.Forms.Masters frm = (Masters.Forms.Masters)GlobalInfo.OpenForm("Plumber.Plumber.Masters.Forms.Masters", false);
                string type = dgwJE.GetValue<string>(e.Row, PL_SalesInvoices.Type);
                frm.FindRecord(new RadarMasterParam(type, e.CellValue));
            }
        }

        public void AutoResizeColumns()
        {
            dgwJE.AutoResizeColumns();
        }

        public void OnPrepareAuxData()
        {
        }

        public void CalculateTotal()
        {
            ntbFrom.Double = 0;
            ntbTo.Double = 0;
            foreach (DataGridViewRow dr in dgwJE.Rows)
            {
                if (!dr.IsNewRow)
                {
                    ntbFrom.Double += DataGridViewRowValues.GetValue<double>(dr, PL_JournalEntryDetail.From);
                    ntbTo.Double += DataGridViewRowValues.GetValue<double>(dr, PL_JournalEntryDetail.To);
                }
            }
        }

        private void dgwJE_DataGridViewDoubleClickCell(object sender, DataGridViewDoubleClickArgs e)
        {
            if (e.ColumnName == PL_JournalEntryDetail.JournalNo.Name)
            {
                if (drJournalEntry.Find(e.CellValue))
                {
                    if (AccountingPref.IsTaxEnabled(saleForm.CtbDate.Today) && 
                        drJournalEntry.GetValue<TaxRegisterType>(PL_JournalEntry.TaxRegister) != TaxRegisterType.E_NORegister)
                    {
                        JournalEntriesSalePurchase frm = (JournalEntriesSalePurchase)GlobalInfo.OpenForm("Plumber.Plumber.Accounting.Forms.JournalEntriesSalePurchase", false);
                        frm.FindRecord(new RadarJournalEntryParam(e.CellValue));
                    }
                    else
                    {
                        JournalEntries frm = (JournalEntries)GlobalInfo.OpenForm("Plumber.Plumber.Accounting.Forms.JournalEntries", false);
                        frm.FindRecord(new RadarJournalEntryParam(e.CellValue));
                    }
                }
            }
        }
    }
}